<?php
/**
 * Created by PhpStorm.
 * User: yy
 * Date: 16-10-27
 * Time: 下午10:09
 */

namespace Ftt;

/**
 * @method static Route get(string $route, Callable $callback, string $name = null)
 * @method static Route post(string $route, Callable $callback, string $name = null)
 * @method static Route any(string $route, Callable $callback, string $name = null)
 * @method static Route put(string $route, Callable $callback, string $name = null)
 * @method static Route delete(string $route, Callable $callback, string $name = null)
 * @method static Route options(string $route, Callable $callback, string $name = null)
 * @method static Route head(string $route, Callable $callback, string $name = null)
 */
class Route
{
    public static $prefix = 'App\\controllers';

    protected static $routes = array();
    protected static $methods = array();
    protected static $callbacks = array();
    protected static $names = array();

    public static function __callStatic($method, $arguments)
    {
        $callback = $arguments[1];
        $name = isset($arguments[2]) ? $arguments[2] : uniqid();

        array_push(self::$routes, $arguments[0]);
        array_push(self::$methods, strtoupper($method));
        array_push(self::$callbacks, $callback);
        array_push(self::$names, $name);

    }

    public static function getUir(){
        $uir = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
        $uir = '/'.rtrim($uir, '/');
        return str_replace('//', '/', $uir);
    }

    public static function dispatch(){

        $method = $_SERVER['REQUEST_METHOD'];

        $uir = self::getUir();

        if(in_array($uir, self::$routes)){

            $route_pos = array_keys(self::$routes, $uir);

            foreach ($route_pos as $route){
                if(self::$methods[$route] == $method){

                    if(is_object(self::$callbacks[$route])){
                        call_user_func(self::$callbacks[$route], $uir);
                    }else{
                        $parts = explode('/', self::$callbacks[$route]);

                        $last = end($parts);

                        $segments = explode('@', $last);

                        $controllerClass = self::$prefix.'\\'.$segments[0];

                        if(!class_exists($controllerClass)){
                            throw new Exception("This class $controllerClass is not exists!");
                        }

                        $controllerObj = new $controllerClass();

                        $action = $segments[1];

                        if(!method_exists($controllerObj, $action)){
                            throw new Exception("This method $controllerClass::$action is not defined!");
                        }

                        $controllerObj->$action();
                    }
                }else{
                    header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
                    throw new Exception("Unknown request route [$method: $uir] !");
                }
            }

        }else{
            header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
            throw new Exception("Unknown request route [$method: $uir] !");
        }
    }
}